# coding: utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import requests
import json

def all_search(driver, tasks, result):
    for task in tasks:
        search(driver, task['id'],task['keywords'], task['asin'], result)

def search(driver, id,keyword, asin, result):
    wait_for_keywords = True
    while wait_for_keywords:
        elem = smart_wait(driver, '#twotabsearchtextbox')  # driver.execute_script("window.stop();")
        try:
            if elem and elem.is_displayed():
                elem.clear()
                print('input clear')
                elem.send_keys(keyword)
                print('keywords input',keyword)
                print('asin to find',asin)
                elem.send_keys(Keys.RETURN)
                wait_for_keywords = False
        except Exception,e:
            print(e)


    not_found = True
    page = 1
    temp = {'id': id, 'page': 0, 'page_ad': 0}
    while not_found:
        smart_wait(driver, 'ul#s-results-list-atf')
        if page == 1:
            sleep(10)
        nodes = driver.find_elements_by_css_selector("ul#s-results-list-atf > li")
        print('nodes get')
        for node in nodes:  # todo 有的时候会没有找全元素，解决办法，在这里循环完毕后，记录最后一个node的id，然后再找一次，找到最后一个为止
            ActionChains(driver).move_to_element(node).perform()
            asin_find = node.get_attribute("data-asin")
            print('watch', asin_find)
            if asin_find == asin:
                print("found element")
                #记录page
                sponsor = None
                try:
                    sponsor = node.find_element_by_css_selector('h5.a-spacing-none.a-color-tertiary.s-sponsored-list-header.s-sponsored-header.sp-pixel-data.a-text-normal')
                except Exception, e:
                    print('not sponsor')

                if sponsor == None:  # 普通链接
                    temp['id'] = id
                    temp['page'] = page
                else:
                    print('is a sponsor')
                    temp['page_ad'] = page

                if temp['page'] != 0 and temp['page_ad'] != 0:
                    not_found = False
                    break

        if not_found:
            if page >= 20:
                not_found = False
                break

            page_bottom = smart_wait(driver, 'div#pagn')
            if page_bottom:
                next_page = page_bottom.find_element_by_css_selector('span#pagnNextString')
                if next_page:
                    ActionChains(driver).move_to_element(next_page).perform()
                    next_page.click()
                    page += 1
                    page_wait(driver, page)
                    print("click page")
                else:
                    # the last page, do something and break
                    not_found = False
                    break

    result.append(temp)
    print("end loop")

def smart_wait(driver, css, wait_time = 60):
    element = None
    for i in range(wait_time):
        if i >= wait_time :
            print("timeout")
            break
        try:
            element = driver.find_element_by_css_selector(css)
            if element:
               print("find element", css)
               break
        except:
            print("wait for find element", css, i)
        sleep(1)
    return element

def page_wait(driver, page, wait_time = 60):
    for i in range(wait_time):
        if i >= wait_time :
            print("timeout")
            break
        try:
            element = driver.find_element_by_css_selector('span.pagnCur')
            if element:
                el_text = element.text.decode('utf-8')
                print("wait to find page...", el_text, str(page))
            if element and el_text == str(page):
                print("find page",el_text)
                break
        except:
            print("wait for find page",i)
        sleep(1)

options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
capa = options.to_capabilities()
capa["pageLoadStrategy"] = "none"
# self.driver = webdriver.Chrome(desired_capabilities=capa)
driver = webdriver.Chrome("./chromedriver", desired_capabilities=capa)
driver.get("https://www.amazon.com")
base_host = "http://127.0.0.1/local"
host = base_host + "/rank/tasks"
tasks = None
client = requests.get(host)
if client.status_code == requests.codes.ok:
    result = client.json()
    if result['status'] == 0:
        tasks = result['data']
    else:
        print result['message']
if tasks != None:
# tasks = "[{'id': 1,'keywords': 'iphone charger','asin':'B07BGBYQM5'},{'id': 2,'keywords':'wine opener','asin':'B01MQO62VV'}]"
    try:
        result = []
        all_search(driver, tasks, result)
        driver.close()
        driver.quit()
        print(result)
        host = base_host + "/insert/capture"
        data = { "result": result }
        print(data)
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        client = requests.post(host, data=json.dumps(data), headers=headers)
        if client.status_code == requests.codes.ok:
            result = client.json()
            if result['status'] == 0:
                print('成功')
            else:
                print('post error',result['message'])
    except Exception, e:
        print('exception', e)
